<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\form\util\control\dctrl\items;

use yii\base\InvalidArgumentException;

/**
 * 下拉选择控件
 * @author cleverstone
 * @since ym2.0
 */
class Select extends Dcontrol
{

    /**
     * @var array
     */
    private $_options = [
        'control' => 'select',
        'label' => '',
        'placeholder' => '请选择',
        'default' => '',
        'options' => [],
        'required' => false,
        'comment' => '',
        'style' => '',
        'class' => '',
        'attribute' => '',
        'multiple' => false,
        'disabled' => [],
        'minWidth' => '',
        'uiConfig' => [
            'allowClear' => true,//允许清除选中
            'width' => '100%',//控件宽
            'selectedInOrder' => true, //多选时,是否按选择顺序排序
            'selectedHideOption' => true, //多选时,是否隐藏已选择过的选项
            'closeOnSelect' => true, //选择后,是否关闭选项内容
        ],
    ];

    /**
     * 选择后,不关闭选项内容
     * @return $this
     */
    public function unCloseOnSelect()
    {
        $this->_options['uiConfig']['closeOnSelect'] = false;
        return $this;
    }

    /**
     * select2 配置
     * @param string $key 配置
     * @param mixed $value 值
     * @return $this
     */
    public function uiConfig($key, $value)
    {
        $this->_options['uiConfig'][$key] = $value;
        return $this;
    }

    /**
     * 单元格最小宽度
     * @param integer|string $minWidth
     * @return $this
     */
    public function minWidth($minWidth)
    {
        if (is_numeric($minWidth)) {
            $minWidth .= 'px';
        }

        $this->_options['minWidth'] = $minWidth;
        return $this;
    }

    /**
     * 禁用项
     * @param array $item
     * @return $this
     */
    public function disabled(array $item)
    {
        $this->_options['disabled'] = $item;
        return $this;
    }

    /**
     * 多选
     * @return $this
     */
    public function multiple()
    {
        $this->_options['multiple'] = true;
        return $this;
    }

    /**
     * 选项
     * @param array $options
     * @return $this
     */
    public function options(array $options)
    {
        $this->_options['options'] = $options;
        return $this;
    }

    /**
     * 控件标签
     * @param string $label
     * @return $this
     */
    public function label($label)
    {
        $this->_options['label'] = (string)$label;
        return $this;
    }

    /**
     * 站位文本
     * @param string $placeholder
     * @return $this
     */
    public function placeholder($placeholder)
    {
        $this->_options['placeholder'] = (string)$placeholder;
        return $this;
    }

    /**
     * 默认值
     * @param string|array $default
     * @return $this
     */
    public function defaultValue($default)
    {
        $this->_options['default'] = $default;
        return $this;
    }

    /**
     * 是否必填
     * @param bool $required
     * @return $this
     */
    public function required($required = true)
    {
        $this->_options['required'] = (boolean)$required;
        return $this;
    }

    /**
     * 字段注释
     * @param string $comment
     * @return $this
     */
    public function comment($comment)
    {
        $this->_options['comment'] = (string)$comment;
        return $this;
    }

    /**
     * CSS样式
     * @param array|string $style
     * @return $this
     */
    public function style($style)
    {
        $this->formatStyle($style);
        $this->_options['style'] = $style;

        return $this;
    }

    /**
     * UI
     * @param array|string $class
     * @return $this
     */
    public function uiClass($class)
    {
        $this->formatUiClass($class);
        $this->_options['class'] = $class;

        return $this;
    }

    /**
     * HTML属性
     * @param array|string $attribute
     * @return $this
     */
    public function attribute($attribute)
    {
        $this->formatHtmlAttribute($attribute);
        $this->_options['attribute'] = $attribute;

        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        // 多选默认值检查
        if ($this->_options['multiple'] && !empty($this->_options['default'])) {
            if (!is_array($this->_options['default'])) {
                throw new InvalidArgumentException('下拉选择是多选时，默认值必须是数组类型！');
            }
        }

        return $this->_options;
    }
}